import { Injectable } from '@angular/core';
import {Http} from "@angular/http";
import MTSConstants from "../config/config";

@Injectable()
export class UserInfoService {

  servicename:string;

  constructor(private http:Http) { }

  //人员开窗
  getUserInfo(pageNumber:number,pageSize:number,queryId:string,queryName:string){
    const url=`${MTSConstants.HOSTUser}openwin/users?pageNumber=${pageNumber}&pageSize=${pageSize}&queryId=${queryId}&queryName=${queryName}`;
    return this.http.get(url)
      .toPromise()
      .then(res=>{return res.json()})
      .catch((error: any) => {return this.handleError('getUserInfo',error);});

  }

  //部门开窗
  getDepartmentInfo(pageNumber:number,pageSize:number,queryId:string,queryName:string){
    const url=`${MTSConstants.HOSTUser}openwin/departments?pageNumber=${pageNumber}&pageSize=${pageSize}&queryId=${queryId}&queryName=${queryName}`;
    return this.http.get(url)
      .toPromise()
      .then(res=>{return res.json()})
      .catch((error: any) => {return this.handleError('getDepartmentInfo',error);});

  }

  //群组开窗
  getGroupInfo(pageNumber:number,pageSize:number,queryId:string,queryName:string){
    const url=`${MTSConstants.HOSTUser}openwin/groups?pageNumber=${pageNumber}&pageSize=${pageSize}&queryId=${queryId}&queryName=${queryName}`;
    return this.http.get(url)
      .toPromise()
      .then(res=>{return res.json()})
      .catch((error: any) => {return this.handleError('getGroupInfo',error);});

  }

  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }

}
